package com.guonl.feign.auth;

import feign.MethodMetadata;
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;

@Component
public class AuthHeaderProcessor implements AnnotatedParameterProcessor {
  private static final Class<Auth> ANNOTATION = Auth.class;
  @Override
  public Class<? extends Annotation> getAnnotationType() {
    return ANNOTATION;
  }

  @Override
  public boolean processArgument(AnnotatedParameterContext context, Annotation annotation, Method method) {
    int parameterIndex = context.getParameterIndex();
//    Class<?> parameterType = method.getParameterTypes()[parameterIndex];
    MethodMetadata data = context.getMethodMetadata();

//
//    if (!AuthUser.class.isAssignableFrom(parameterType)) {
//      throw new IllegalStateException(ANNOTATION.getSimpleName() + " 必须配合 " + AuthUser.class.getName() + " 使用");
//    }

    data.ignoreParamater(parameterIndex);

    context.setParameterName("user_id");
    Collection<String> header = context.setTemplateParameter("user_id", data.template().headers().get("wh-user-id"));
    data.template().header("wh-user-id", header);

    return true;
  }
}
